home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / PURIFY.C < prev    next >
C/C++ Source or Header  |  1992-02-18  |  17KB  |  585 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /tmp/scm386/microcode/RCS/purify.c,v 9.47 1992/02/18 17:30:22 jinx Exp $
  4.  
  5. Copyright (c) 1988-1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* This file contains the code that copies objects into pure
  36.    and constant space. */
  37.  
  38. #include "scheme.h"
  39. #include "prims.h"
  40. #include "gccode.h"
  41. #include "zones.h"
  42.  
  43. /* Imports */
  44.  
  45. extern void EXFUN (GCFlip, (void));
  46. extern void EXFUN (GC, (void));
  47. extern SCHEME_OBJECT * EXFUN (GCLoop, (SCHEME_OBJECT *, SCHEME_OBJECT **));
  48.  
  49. /* This is a copy of GCLoop, with mode handling added, and
  50.    debugging printout removed.
  51. */
  52.  
  53. /* Purify modes */
  54.  
  55. #define    NORMAL_GC    0
  56. #define PURE_COPY    1
  57. #define CONSTANT_COPY    2
  58.  
  59. #define Purify_Pointer(Code)                        \
  60. {                                    \
  61.   Old = (OBJECT_ADDRESS (Temp));                    \
  62.   if ((GC_Mode == CONSTANT_COPY) &&                    \
  63.       (Old > Low_Constant))                        \
  64.     continue;                                \
  65.   Code;                                    \
  66. }
  67.  
  68. #define Setup_Pointer_for_Purify(Extra_Code)                \
  69. {                                    \
  70.   Purify_Pointer(Setup_Pointer(false, Extra_Code));            \
  71. }
  72.  
  73. #define Indirect_BH(In_GC)                        \
  74. {                                    \
  75.   if ((OBJECT_TYPE (*Old)) == TC_BROKEN_HEART)                \
  76.     continue;                                \
  77. }
  78.  
  79. #define Transport_Vector_Indirect()                    \
  80. {                                    \
  81.   Real_Transport_Vector ();                        \
  82.   *(OBJECT_ADDRESS (Temp)) = New_Address;                \
  83. }
  84.  
  85. SCHEME_OBJECT *
  86. DEFUN (PurifyLoop,
  87.        (Scan, To_Pointer, GC_Mode),
  88.        fast SCHEME_OBJECT *Scan AND
  89.        SCHEME_OBJECT **To_Pointer AND
  90.        int GC_Mode)
  91. {
  92.   fast SCHEME_OBJECT *To, *Old, Temp, *Low_Constant, New_Address;
  93.  
  94.   To = *To_Pointer;
  95.   Low_Constant = Constant_Space;
  96.   for ( ; Scan != To; Scan++)
  97.   {
  98.     Temp = *Scan;
  99.     Switch_by_GC_Type(Temp)
  100.     {
  101.       case TC_BROKEN_HEART:
  102.         if (Scan == (OBJECT_ADDRESS (Temp)))
  103.     {
  104.       *To_Pointer = To;
  105.       return Scan;
  106.     }
  107.     sprintf(gc_death_message_buffer,
  108.         "purifyloop: broken heart (0x%lx) in scan",
  109.         Temp);
  110.     gc_death(TERM_BROKEN_HEART, gc_death_message_buffer, Scan, To);
  111.     /*NOTREACHED*/
  112.  
  113.       case TC_MANIFEST_NM_VECTOR:
  114.       case TC_MANIFEST_SPECIAL_NM_VECTOR:
  115.     Scan += OBJECT_DATUM (Temp);
  116.     break;
  117.  
  118.       /* Compiled code relocation. */
  119.  
  120.       case TC_LINKAGE_SECTION:
  121.       {
  122.     if (GC_Mode == PURE_COPY)
  123.     {
  124.       gc_death(TERM_COMPILER_DEATH,
  125.            "purifyloop: linkage section in pure area",
  126.            Scan, To);
  127.       /*NOTREACHED*/
  128.     }
  129.  
  130.     switch (READ_LINKAGE_KIND (Temp))
  131.     {
  132.       case REFERENCE_LINKAGE_KIND:
  133.       case ASSIGNMENT_LINKAGE_KIND:
  134.       {
  135.         /* Assumes that all others are objects of type TC_QUAD without
  136.            their type codes.
  137.          */
  138.  
  139.         fast long count;
  140.  
  141.         Scan++;
  142.         for (count = READ_CACHE_LINKAGE_COUNT(Temp);
  143.          --count >= 0;
  144.          Scan += 1)
  145.         {
  146.           Temp = *Scan;
  147.           Setup_Pointer_for_Purify(Transport_Quadruple());
  148.         }
  149.         Scan -= 1;
  150.         break;
  151.       }
  152.  
  153.       case OPERATOR_LINKAGE_KIND:
  154.       case GLOBAL_OPERATOR_LINKAGE_KIND:
  155.       {
  156.         fast long count;
  157.         fast char *word_ptr;
  158.         SCHEME_OBJECT *end_scan;
  159.  
  160.         START_OPERATOR_RELOCATION (Scan);
  161.         count = (READ_OPERATOR_LINKAGE_COUNT (Temp));
  162.         word_ptr = (FIRST_OPERATOR_LINKAGE_ENTRY (Scan));
  163.         end_scan = (END_OPERATOR_LINKAGE_AREA (Scan, count));
  164.  
  165.         while(--count >= 0)
  166.         {
  167.           Scan = ((SCHEME_OBJECT *) word_ptr);
  168.           word_ptr = (NEXT_LINKAGE_OPERATOR_ENTRY (word_ptr));
  169.           EXTRACT_OPERATOR_LINKAGE_ADDRESS (Temp, Scan);
  170.           Purify_Pointer(Setup_Internal(false,
  171.                         Transport_Compiled(),
  172.                         Compiled_BH(false,
  173.                             goto next_operator)));
  174.           next_operator:
  175.           STORE_OPERATOR_LINKAGE_ADDRESS(Temp, Scan);
  176.         }
  177.         Scan = end_scan;
  178.         END_OPERATOR_RELOCATION (Scan);
  179.         break;
  180.       }
  181.  
  182.       default:
  183.       {
  184.         gc_death (TERM_EXIT,
  185.               "purifyloop: Unknown compiler linkage kind.",
  186.               Scan, Free);
  187.         /*NOTREACHED*/
  188.       }
  189.     }
  190.     break;
  191.       }
  192.  
  193.       case TC_MANIFEST_CLOSURE:
  194.       {
  195.     fast long count;
  196.     fast char *word_ptr;
  197.     SCHEME_OBJECT *area_end;
  198.  
  199.     if (GC_Mode == PURE_COPY)
  200.     {
  201.       gc_death(TERM_COMPILER_DEATH,
  202.            "purifyloop: manifest closure in pure area",
  203.            Scan, To);
  204.       /*NOTREACHED*/
  205.     }
  206.  
  207.     START_CLOSURE_RELOCATION (Scan);
  208.     Scan += 1;
  209.     count = (MANIFEST_CLOSURE_COUNT (Scan));
  210.     word_ptr = (FIRST_MANIFEST_CLOSURE_ENTRY (Scan));
  211.     area_end = (MANIFEST_CLOSURE_END (Scan, count));
  212.  
  213.     while ((--count) >= 0)
  214.     {
  215.       Scan = ((SCHEME_OBJECT *) (word_ptr));
  216.       word_ptr = (NEXT_MANIFEST_CLOSURE_ENTRY (word_ptr));
  217.       EXTRACT_CLOSURE_ENTRY_ADDRESS (Temp, Scan);
  218.       Purify_Pointer(Setup_Internal(false,
  219.                     Transport_Compiled(),
  220.                     Compiled_BH(false,
  221.                             goto next_closure)));
  222.     next_closure:
  223.       STORE_CLOSURE_ENTRY_ADDRESS(Temp, Scan);
  224.     }
  225.     Scan = area_end;
  226.     END_CLOSURE_RELOCATION (Scan);
  227.     break;
  228.       }
  229.  
  230.       case_compiled_entry_point:
  231.     if (GC_Mode != PURE_COPY)
  232.     {
  233.       Purify_Pointer(Setup_Internal(false,
  234.                     Transport_Compiled(),
  235.                     Compiled_BH(false, goto after_entry)));
  236.         after_entry:
  237.       *Scan = Temp;
  238.     }
  239.     break;
  240.  
  241.       case_Cell:
  242.     Setup_Pointer_for_Purify(Transport_Cell());
  243.     break;
  244.  
  245.       case TC_WEAK_CONS:
  246.     Setup_Pointer_for_Purify(Transport_Weak_Cons());
  247.     break;
  248.  
  249.       /*
  250.     Symbols, variables, and reference traps cannot be put into
  251.     pure space.  The strings contained in the first two can, on the
  252.     other hand.
  253.        */
  254.  
  255.       case TC_REFERENCE_TRAP:
  256.     if ((OBJECT_DATUM (Temp) <= TRAP_MAX_IMMEDIATE) ||
  257.         (GC_Mode == PURE_COPY))
  258.     {
  259.       /* It is a non pointer. */
  260.       break;
  261.     }
  262.     goto purify_pair;
  263.  
  264.       case TC_INTERNED_SYMBOL:
  265.       case TC_UNINTERNED_SYMBOL:
  266.     if (GC_Mode == PURE_COPY)
  267.         {
  268.       Temp = MEMORY_REF (Temp, SYMBOL_NAME);
  269.       Purify_Pointer(Setup_Internal(false,
  270.                     Transport_Vector_Indirect(),
  271.                     Indirect_BH(false)));
  272.       break;
  273.     }
  274.  
  275.     /* Fall through */
  276.  
  277.       case_Fasdump_Pair:
  278.       purify_pair:
  279.     Setup_Pointer_for_Purify(Transport_Pair());
  280.     break;
  281.  
  282.       case TC_VARIABLE:
  283.       case_Triple:
  284.     Setup_Pointer_for_Purify(Transport_Triple());
  285.     break;
  286.  
  287.       case_Quadruple:
  288.     Setup_Pointer_for_Purify(Transport_Quadruple());
  289.     break;
  290.  
  291.       case TC_BIG_FLONUM:
  292.         Setup_Pointer_for_Purify({
  293.       Transport_Flonum();
  294.       break;
  295.     });
  296.  
  297.     /* No need to handle futures specially here, since PurifyLoop
  298.        is always invoked after running GCLoop, which will have
  299.        spliced all spliceable futures unless the GC itself of the
  300.        GC dameons spliced them, but this should not occur.
  301.      */
  302.  
  303.       case TC_FUTURE:
  304.       case TC_ENVIRONMENT:
  305.       case TC_COMPILED_CODE_BLOCK:
  306.     if (GC_Mode == PURE_COPY)
  307.     {
  308.       /* For environments, this should actually do an indirect pair
  309.          transport of the procedure, at least.
  310.        */
  311.       break;
  312.     }
  313.     /* Fall through */
  314.  
  315.       case_Purify_Vector:
  316.     Setup_Pointer_for_Purify(Transport_Vector());
  317.     break;
  318.  
  319.       default:
  320.     GC_BAD_TYPE("purifyloop");
  321.     /* Fall Through */
  322.  
  323.       case_Non_Pointer:
  324.     break;
  325.  
  326.       } /* Switch_by_GC_Type */
  327.   } /* For loop */
  328.  
  329.   *To_Pointer = To;
  330.   return (To);
  331.  
  332. } /* PurifyLoop */
  333.  
  334. /* Description of the algorithm for PURIFY:
  335.  
  336.    The algorithm is trickier than would first appear necessary.  This
  337.    is because the size of the object being purified must be
  338.    calculated.  The idea is that the entire object is copied into the
  339.    new heap, and then a normal GC is done (the broken hearts created
  340.    by the copy will, of course, now be used to relocate references to
  341.    parts of the object).  If there is not enough room in constant
  342.    space for the object, processing stops with a #!false return and
  343.    the world flipped into the new heap.  Otherwise, the
  344.    process is repeated, moving the object into constant space on the
  345.    first pass and then doing a GC back into the original heap.
  346.  
  347.    Notice that in order to make a pure object, the copy process
  348.    proceeds in two halves.  During the first half (which collects the
  349.    pure part) Compiled Code, Environments, Symbols, and Variables
  350.    (i.e.  things whose contents change) are NOT copied.  Then a header
  351.    is put down indicating constant (not pure) area, and then they ARE
  352.    copied.
  353.  
  354.    The constant area contains a contiguous set of blocks of the
  355.    following format:
  356.  
  357.   >>Top of Memory (Stack above here)<<
  358.  
  359.                    . (direction of growth)
  360.                    .  ^
  361.                    . / \
  362.                    .  |
  363.                    .  |
  364.         |----------------------|...
  365.         | END   | Total Size M |   . Where END   = TC_FIXNUM
  366.         |----------------------|    .      SNMH  = TC_MANIFEST_SPECIAL_...
  367.         | SNMH  |      1       |    |      CONST = TC_TRUE
  368.         |----------------------|    |      PURE  = TC_FALSE
  369.         |                      |    |
  370.         |                      |    |
  371.         |    CONSTANT AREA     |    |
  372.         |                      |    |
  373.         |                      |     .
  374.      ...|----------------------|      >  M
  375.     .   | CONST | Pure Size N  |     .
  376.    .    |----------------------|    |
  377.    |    | SNMH  |      1       |    |
  378.    |    |----------------------|    |
  379.    |    |                      |    |
  380. N <     |                      |    |
  381.    |    |      PURE AREA       |    |
  382.    |    |                      |    |
  383.    .    |                      |    .
  384.     .   |----------------------|   .
  385.      ...| PURE  | Total Size M |...
  386.         |----------------------|
  387.         | SNMH  | Pure Size N  |
  388.         |----------------------|
  389.  
  390.   >>Base of Memory (Heap below here)<<
  391. */
  392.  
  393. /* The result returned by Purify is a vector containing this data */
  394.  
  395. #define Purify_Vector_Header    0
  396. #define Purify_Length        1
  397. #define Purify_Really_Pure    2
  398. #define Purify_N_Slots        2
  399.  
  400. SCHEME_OBJECT
  401. DEFUN (Purify,
  402.        (Object, Purify_Object),
  403.        SCHEME_OBJECT Object AND
  404.        SCHEME_OBJECT Purify_Object)
  405. {
  406.   long Length;
  407.   SCHEME_OBJECT *Heap_Start, *Result, Answer;
  408.  
  409. /* Pass 1 -- Copy object to new heap, then GC into that heap */
  410.  
  411.   GCFlip ();
  412.   Heap_Start = Free;
  413.   *Free++ = Object;
  414.   Result = GCLoop (Heap_Start, &Free);
  415.   if (Free != Result)
  416.   {
  417.     fprintf (stderr, "\nPurify: Pure Scan ended too early.\n");
  418.     Microcode_Termination (TERM_BROKEN_HEART);
  419.   }
  420.   Length = ((Free - Heap_Start) - 1);        /* Length of object */
  421.   GC ();
  422.   Free[Purify_Vector_Header] =
  423.     MAKE_OBJECT (TC_MANIFEST_VECTOR, Purify_N_Slots);
  424.   Free[Purify_Length] = LONG_TO_UNSIGNED_FIXNUM(Length);
  425.   Free[Purify_Really_Pure] = Purify_Object;
  426.   Answer =  MAKE_POINTER_OBJECT (TC_VECTOR, Free);
  427.   Free += (Purify_N_Slots + 1);
  428.   return (Answer);
  429. }
  430.  
  431. SCHEME_OBJECT
  432. DEFUN (Purify_Pass_2,
  433.        (Info),
  434.        SCHEME_OBJECT Info)
  435. {
  436.   long Length;
  437.   Boolean Purify_Object;
  438.   SCHEME_OBJECT *New_Object, Relocated_Object, *Result;
  439.   long Pure_Length, Recomputed_Length;
  440.  
  441.   STACK_SANITY_CHECK ("PURIFY");
  442.   Length = (OBJECT_DATUM (FAST_MEMORY_REF (Info, Purify_Length)));
  443.   if (FAST_MEMORY_REF (Info, Purify_Really_Pure) == SHARP_F)
  444.   {
  445.     Purify_Object =  false;
  446.   }
  447.   else
  448.   {
  449.     Purify_Object = true;
  450.   }
  451.   Relocated_Object = *Heap_Bottom;
  452.   if (!(TEST_CONSTANT_TOP (Free_Constant + Length + 6)))
  453.   {
  454.     return (SHARP_F);
  455.   }
  456.   New_Object = Free_Constant;
  457.   GCFlip ();
  458.   *Free_Constant++ = SHARP_F;    /* Will hold pure space header */
  459.   *Free_Constant++ = Relocated_Object;
  460.   if (Purify_Object)
  461.   {
  462.     Result = PurifyLoop ((New_Object + 1), &Free_Constant, PURE_COPY);
  463.  
  464.     if (Free_Constant != Result)
  465.     {
  466.       fprintf (stderr, "\nPurify: Pure Copy ended too early.\n");
  467.       Microcode_Termination (TERM_BROKEN_HEART);
  468.     }
  469.     Pure_Length = ((Free_Constant - New_Object) + 1);
  470.   }
  471.   else
  472.   {
  473.     Pure_Length = 3;
  474.   }
  475.   *Free_Constant++ = (MAKE_OBJECT (TC_MANIFEST_SPECIAL_NM_VECTOR, 1));
  476.   *Free_Constant++ = (MAKE_OBJECT (CONSTANT_PART, Pure_Length));
  477.   if (Purify_Object)
  478.   {
  479.     Result = PurifyLoop ((New_Object + 1), &Free_Constant, CONSTANT_COPY);
  480.     if (Result != Free_Constant)
  481.     {
  482.       fprintf (stderr, "\nPurify: Pure Copy ended too early.\n");
  483.       Microcode_Termination (TERM_BROKEN_HEART);
  484.     }
  485.   }
  486.  
  487. /* Purify_Pass_2 continues on the next page */
  488.  
  489. /* Purify_Pass_2, continued */
  490.  
  491.   else
  492.   {
  493.     Result = GCLoop ((New_Object + 1), &Free_Constant);
  494.     if (Result != Free_Constant)
  495.     {
  496.       fprintf (stderr, "\nPurify: Constant Copy ended too early.\n");
  497.       Microcode_Termination (TERM_BROKEN_HEART);
  498.     }
  499.   }
  500.   Recomputed_Length = ((Free_Constant - New_Object) - 4);
  501.   *Free_Constant++ = (MAKE_OBJECT (TC_MANIFEST_SPECIAL_NM_VECTOR, 1));
  502.   *Free_Constant++ = (MAKE_OBJECT (END_OF_BLOCK, (Recomputed_Length + 5)));
  503.   if (!(TEST_CONSTANT_TOP (Free_Constant)))
  504.   {
  505.     fprintf (stderr,
  506.          "\nPurify overrun: Constant_Top = 0x%lx, Free_Constant = 0x%lx\n",
  507.          Constant_Top, Free_Constant);
  508.     Microcode_Termination (TERM_EXIT);
  509.   }
  510.   *New_Object++ =
  511.     (MAKE_OBJECT (TC_MANIFEST_SPECIAL_NM_VECTOR, Pure_Length));
  512.   *New_Object = (MAKE_OBJECT (PURE_PART, (Recomputed_Length + 5)));
  513.   SET_CONSTANT_TOP ();
  514.   GC ();
  515.   return (SHARP_T);
  516. }
  517.  
  518. /* (PRIMITIVE-PURIFY OBJECT PURE? SAFETY-MARGIN)
  519.    Copy an object from the heap into constant space.  This requires
  520.    a spare heap, and is tricky to use -- it should only be used
  521.    through the wrapper provided in the Scheme runtime system.
  522.  
  523.    To purify an object we just copy it into Pure Space in two
  524.    parts with the appropriate headers and footers.  The actual
  525.    copying is done by PurifyLoop above.
  526.  
  527.    Once the copy is complete we run a full GC which handles the
  528.    broken hearts which now point into pure space.  On a
  529.    multiprocessor, this primitive uses the master-gc-loop and it
  530.    should only be used as one would use master-gc-loop i.e. with
  531.    everyone else halted.
  532.  
  533.    This primitive does not return normally.  It always escapes into
  534.    the interpreter because some of its cached registers (eg. History)
  535.    have changed.
  536. */
  537.  
  538. DEFINE_PRIMITIVE ("PRIMITIVE-PURIFY", Prim_primitive_purify, 3, 3, 0)
  539. {
  540.   long new_gc_reserve;
  541.   SCHEME_OBJECT Object, Purify_Result, Daemon;
  542.   PRIMITIVE_HEADER (3);
  543.   PRIMITIVE_CANONICALIZE_CONTEXT ();
  544.  
  545.   STACK_SANITY_CHECK ("PURIFY");
  546.   Save_Time_Zone (Zone_Purify);
  547.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), Object);
  548.   CHECK_ARG (2, BOOLEAN_P);
  549.   new_gc_reserve = (arg_nonnegative_integer (3));
  550.  
  551.   /* Pass 1 (Purify, above) does a first copy.  Then any GC daemons
  552.      run, and then Purify_Pass_2 is called to copy back.
  553.   */
  554.  
  555.   GC_Reserve = new_gc_reserve;
  556.   ENTER_CRITICAL_SECTION ("purify pass 1");
  557.   Purify_Result = (Purify (Object, (ARG_REF (2))));
  558.   POP_PRIMITIVE_FRAME (3);
  559.   Daemon = (Get_Fixed_Obj_Slot (GC_Daemon));
  560.   if (Daemon == SHARP_F)
  561.   {
  562.     SCHEME_OBJECT words_free;
  563.  
  564.     RENAME_CRITICAL_SECTION ("purify pass 2");
  565.     Purify_Result = (Purify_Pass_2 (Purify_Result));
  566.     words_free = (LONG_TO_UNSIGNED_FIXNUM (MemTop - Free));
  567.     Val = (MAKE_POINTER_OBJECT (TC_LIST, Free));
  568.     (*Free++) = Purify_Result;
  569.     (*Free++) = words_free;
  570.     PRIMITIVE_ABORT (PRIM_POP_RETURN);
  571.     /*NOTREACHED*/
  572.   }
  573.  
  574.   RENAME_CRITICAL_SECTION ("purify daemon 1");
  575.   Store_Expression (Purify_Result);
  576.   Store_Return (RC_PURIFY_GC_1);
  577.  Will_Push (CONTINUATION_SIZE + STACK_ENV_EXTRA_SLOTS + 1);
  578.   Save_Cont ();
  579.   STACK_PUSH (Daemon);
  580.   STACK_PUSH (STACK_FRAME_HEADER);
  581.  Pushed ();
  582.   PRIMITIVE_ABORT (PRIM_APPLY);
  583.   /*NOTREACHED*/
  584. }
  585.